package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.OrderStatusConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.AdminOrderMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.Order_detailMapper;
import com.sky.result.PageResult;
import com.sky.service.AdminOrderService;
import com.sky.vo.OrderDetailsVO;
import com.sky.vo.OrderStatisticsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Slf4j
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    private AdminOrderMapper adminOrderMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private Order_detailMapper orderDetailMapper;
    @Transactional
    public void confirm(Integer id){
        adminOrderMapper.confirm(id);
    }

    @Transactional
    public void reject(OrdersRejectionDTO dto) {
        //更新状态和拒单原因
        Orders orders = new Orders();
        orders.setId(dto.getId());
        orders.setRejectionReason(dto.getRejectionReason());
        orders.setStatus(OrderStatusConstant.rejection);
        adminOrderMapper.update(orders);
    }

    /**
     * 根据订单id获取订单详情
     * @param id
     */
    public OrderDetailsVO getdetails(String id){
        Orders orders = orderMapper.getByid(Integer.parseInt(id));
        List<OrderDetail> orderDetailList = orderDetailMapper.getByorderID(Integer.parseInt(id));

        OrderDetailsVO vo = new OrderDetailsVO();
        BeanUtils.copyProperties(orders,vo);
        vo.setOrderDetailList(orderDetailList);

        log.info("vo:{}",vo);
        return vo;
    }

    @Transactional
    /**
     * 取消订单
     */
    public  void cancel(OrdersCancelDTO dto){
        //设置订单状态，取消时间，取消原因
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto,orders);
        orders.setCancelTime(LocalDateTime.now());
        orders.setStatus(OrderStatusConstant.Canceled);
        adminOrderMapper.update(orders);
    }

    public PageResult search(OrdersPageQueryDTO dto){
        log.info("dto:{}",dto);
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        if(dto.getBeginTime()!=null && dto.getEndTime()!=null){
            dto.setBeginTime_(LocalDateTime.parse(dto.getBeginTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            dto.setEndTime_(LocalDateTime.parse(dto.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }

        Page page = orderMapper.search(dto);

        Long totals = page.getTotal();
        List<Orders> ordersList = page.getResult();

        PageResult pageResult = new PageResult(totals,ordersList);
        return pageResult;
    }

    /**
     * 完成订单
     * @param id
     */
    @Transactional
    public void complete(String id) {
        Orders orders = new Orders();
        orders.setId(Long.parseLong(id));
        orders.setStatus(OrderStatusConstant.Finshed);
        orders.setDeliveryTime(LocalDateTime.now());//完成订单时，送达时间改为完成订单时间。
        orderMapper.update(orders);
    }

    /**
     * 返回派送中，待接单，待派送的订单数量
     * @return
     */
    public OrderStatisticsVO get(){
        //获取三个状态的订单
        List<Orders> ordersList = orderMapper.getStatistics();
        Integer confirmed = 0;//待派送
        Integer deliveryInProgress = 0;//派送中
        Integer toBeConfirmed = 0;//待接单
        //便利，统计每个状态的订单
        for(Orders orders:ordersList){
            if(orders.getStatus()==OrderStatusConstant.ToBeDelivered)confirmed+=1;
            else if(orders.getStatus()==OrderStatusConstant.OrdersTaken)toBeConfirmed+=1;
            else if(orders.getStatus()==OrderStatusConstant.Delivering)deliveryInProgress+=1;
        }
        //封装结果
        OrderStatisticsVO vo = new OrderStatisticsVO(toBeConfirmed,confirmed,deliveryInProgress);
        return vo;
    }

    /**
     * 派送
     * @param id
     */
    @Transactional
    public void delivery(String id){
        Orders orders = new Orders();
        orders.setId(Long.parseLong(id));
        orders.setStatus(OrderStatusConstant.Delivering);
        orderMapper.update(orders);
    }
}
